/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package TextNor.Expand;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

/**
 *
 * @author Pham_Thanh
 * Tinh xac suat p(e/f)--xac suat tu viết tắt khi đã biết từ đầy đủ.
 */
public class Pef {

    private HashMap fullHm = new HashMap();

//    public static void main(String argv[]) throws UnsupportedEncodingException, FileNotFoundException, IOException {
//        Pef pef = new Pef();
//        System.out.println(pef.calculate("TPHCM", "Thành phố Hồ Chí Minh"));
//    }

    public Pef() {

        System.out.println("package TextNor.Expand; class Pef");

        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("data\\expand\\expn_full.db"), "UTF8"));
            String line=null;
            while ((line = br.readLine()) != null) {
                StringTokenizer st = new StringTokenizer(line, "|");
                while (st.hasMoreTokens()) {
                    String expn = st.nextToken();
                    String full = st.nextToken();
                    int count = Integer.parseInt(st.nextToken());

                    if (fullHm.containsKey(full)) {
                        HashMap expn_countHm = (HashMap) fullHm.get(full);
                        expn_countHm.put(expn, count);
                    } else {
                        HashMap expn_countHm = new HashMap();
                        expn_countHm.put(expn, count);
                        fullHm.put(full, expn_countHm);
                    }
                }
            }
        } catch (Exception e) {
        }

    }
//Tinh p(e/f)

    public double calculate(String expn, String full) {
        HashMap<String, Integer> expn_countHm = (HashMap) fullHm.get(full);
        int total = 0;
        for (Map.Entry<String, Integer> e : expn_countHm.entrySet()) {
            total += e.getValue();
//            String name = e.getKey();
        }
        int count = expn_countHm.get(expn);
        return (double) count / (double) total;

    }
}

